﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ImmunityBuster._2D;
using Microsoft.Xna.Framework;
using TappUp_Refresh;
using Rushabh.Logic.Missiles;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Rushabh.Logic.ArtificialIntelligence
{
    public class AIEngine
    {
        public static List<Missile> missiles = new List<Missile>() ;
        ContentManager content;

        public void setContentManager(ContentManager content)
        {
            this.content = content;
        }

        public List<Enemy2D.IEnemy> MaintainDistance(List<Enemy2D.IEnemy> list, ref Vector2 RefPosition)
        {

            foreach (Enemy2D.IEnemy enemy in list)
            {
                if (enemy.GetHealth() < 95)
                {
                    Sprite enemyz = enemy.GetEnemy();
                    enemyz.flag = true;
                    enemy.SetEnemy(enemyz);
                }

                if (Vector2.Distance(enemy.GetPos(), RefPosition) < 220)
                {
                    Sprite enemyz = enemy.GetEnemy();
                    enemyz.flag = true;
                    enemy.SetEnemy(enemyz);
                }

                if (enemy.GetEnemy().flag == true)
                {
                    Vector2 vel = enemy.getVelocity();
                    Vector2 pos = enemy.GetPos();

                    if (pos.X > RefPosition.X)
                        vel.X = -1;

                    else
                        if (pos.X <= RefPosition.X)
                            vel.X = +1;


                    //pos.Y = 1;
                    if (pos.Y > RefPosition.Y)
                        vel.Y = -1;

                    else
                        if (pos.Y < RefPosition.Y)
                            vel.Y = 1;


                    enemy.SetVelocity(vel);
                }
            }
            return list;
        }

        public void Shoot(List<Enemy2D.IEnemy> list, ref Vector2 RefPosition)
        {
            foreach (ImmunityBuster._2D.Enemy2D.IEnemy enemy in list)
            {
                Sprite temp = enemy.GetEnemy();
                if (temp.flag)
                {
                    if (Randomizer.Randomize(0, 500) > 495)
                    {
                        Missile missile = new Missile(content, temp.Position, RefPosition);
                        missile.Initialize();
                        missiles.Add(missile);
                    }
                }

               
            }          
        
        }

        public void Update(GameTimerEventArgs e)
        {
            foreach (Missile missile in missiles)
            {
                if (missile._position.X < 0 || missile._position.X > 800 || missile._position.Y < 0 || missile._position.Y > 480)
                {
                    missile.active = false;
                }
                missile.Update(e);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Missile missile in missiles)
            {
                missile.Draw(spriteBatch);
            }
        }
    }


    /// <summary>
    /// Does the same thing as Random Function, only difference is that its static, therefore u dont have to create objects!
    /// </summary>
    internal class Randomizer : Random
    {
        public static int Randomize()
        {
            return new Random().Next();
        }

        public static int Randomize(int a, int b)
        {
            return new Random().Next(a, b);
        }
    }
}
    